package Code;

import java.io.*;
import java.util.Scanner;

public class QuestionNum_23_2_hxj {
    public static int num = 0;
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件夹地址:");
        String str = sc.nextLine();
        File file = new File(str);
        searchDir(file);
        System.out.println("txt文件的个数是："+num);
    }

    private static void searchDir(File file) throws Exception{
        File[] files = file.listFiles();
        // 判断传入的文件是否存在
        if (file.exists()) {
            File[] fileArr = file.listFiles();
            for (File f : fileArr) {
                // 判断是否是文件夹
                if (f.isDirectory()) {
                    searchDir(f);
                }
                // 不是文件夹的话,判断是否是txt
                if (f.getName().endsWith("txt")) {
                    num += 1;
//                    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
//                    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
//                    int len;
//                    while ((len = in.read()) != -1) {
//                        out.write(len);
//                    }

//                    in.close();
//                    out.close();
                }

            }
        }
    }
}
